package com.ruoyi.web.controller.system;


import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.web.service.MobileLoginService;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.system.domain.LoginParams;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * 登录控制器
 *
 * @author SK
 * @since 2018/6/13
 */
@RestController
@RequestMapping("/mobile/login")
public class LoginController{

    @Autowired(required = false)
    private TokenService tokenService;

    @Autowired(required = false)
    private MobileLoginService loginService;
    @Autowired
    private ISysUserService userService;


    /**
     * 新增用户
     */

    @PostMapping("/registerUserPwd")
    @ResponseBody
    public AjaxResult registerUserPwd(HttpServletRequest request)
    {
        synchronized (this){
            AjaxResult ajax = AjaxResult.success("注册成功！");
            String userName = request.getParameter("userName");
            String password = request.getParameter("password");
            if (UserConstants.NOT_UNIQUE.equals(userService.checkUserNameUnique(userName)))
            {
                ajax= AjaxResult.error("新增用户'" + userName + "'失败，登录账号已存在");
            }else {
                SysUser user=new SysUser();
                user.setUserName(userName);

                user.setCreateBy("app注册");
                user.setPassword(SecurityUtils.encryptPassword(password));
                user.setDeptId(212L); //一般用户
                user.setRoleId(102L); //普通用户
//                user.setRoleIds(new Long[]{102L});
                Long [] roleIds=new Long[1];
                roleIds[0]=102L;
                user.setRoleIds(roleIds);
                if(userService.insertUser(user)>0){
                    ajax = AjaxResult.success("注册成功！");
                }else {
                    ajax = AjaxResult.error("注册失败！");
                }
            }

            return ajax;
        }


    }

    /**
     * 会员登录
     * @return -1 用户名或密码错误  -2 账号冻结  -3 账号锁定 1 成功  -4 验证码错误
     */
//    @PostMapping("/loginByPassword")
//    @ResponseBody
//    public AjaxResult loginByPassword(HttpServletRequest request) {
//        String username = request.getParameter("username");
//        String password = request.getParameter("password");
//        String phoneNo = request.getParameter("phoneNo");
//        String validCode = request.getParameter("validCode");
//        String loginType = request.getParameter("loginType");
//        // 登录结果
//        LoginParams loginParams = new LoginParams();
//        loginParams.setUsername(username);
//        loginParams.setPassword(password);
//        loginParams.setPhoneNo(phoneNo);
//        loginParams.setValidCode(validCode);
//        loginParams.setLoginType(loginType);
//        return loginService.login(loginParams);
//    }


    @PostMapping("/loginByPassword")
    @ResponseBody
    public AjaxResult loginByPassword(@RequestBody LoginParams loginParams) {

        return loginService.login(loginParams);
    }
    /**
     * 发送验证码
     */
    @PostMapping("/sendCode")
    @ResponseBody
    public AjaxResult sendRegisterCode(HttpServletRequest request) {
        String phoneNo = request.getParameter("phoneNo");
        String validCodeType = request.getParameter("validCodeType");
        // 登录结果
        LoginParams loginParams = new LoginParams();
        loginParams.setPhoneNo(phoneNo);
        loginParams.setValidCodeType(validCodeType);
        return loginService.sendCode(loginParams);
    }

    @GetMapping("/logout")
    @ResponseBody
    public AjaxResult logout(HttpServletRequest request) {
        LoginUser loginUser = tokenService.getLoginUser(request);
        if(null != loginUser){
            tokenService.delLoginUser(loginUser.getToken());
        }
        return AjaxResult.success("退出成功！");
    }

}
